import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Home extends StatefulWidget {
  _ContentWidgetState createState() => new _ContentWidgetState();
}

class _ContentWidgetState extends State<Home> {
  static const methodChannel = const MethodChannel('channel_home');

  // 获取初始化参数
  _initWithPrama() async {
    try {
      var prama =
          await methodChannel.invokeMethod('initWithPrama', '我是flutter传递过来的参数');
      print("parameStr参数获取成功：$prama");
    } on PlatformException {
      print("parameStr参数获取失败");
    }
  }

  Future<dynamic> _platformCallHandler(MethodCall call) async {
    switch (call.method) {
      case 'nativeCallFlutter':
        print('nativeCallFlutter : arguments = ${call.arguments}');
        return Future.value('flutter返回给原生的参数');
      //return Future.error('error message!!');
      default:
        print('Unknowm method ${call.method}');
        throw MissingPluginException();
        break;
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _initWithPrama();

    methodChannel.setMethodCallHandler(_platformCallHandler);
  }

  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter主页面'),
        ),
        body: Center(
          child: Center(
            child: Text('Flutter主页面'),
          ),
        ));
  }
}
